package cn.tedu.egweibo1.controller;

import cn.tedu.egweibo1.mapper.WeiboMapper;
import cn.tedu.egweibo1.pojo.dto.WeiboDTO;
import cn.tedu.egweibo1.pojo.entity.Weibo;
import cn.tedu.egweibo1.pojo.vo.UserSelectByIdVO;
import cn.tedu.egweibo1.pojo.vo.WeiboDetailVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;
    @PostMapping("insert")
    public int insert(@RequestBody WeiboDTO weiboDTO, HttpSession session){
        UserSelectByIdVO userSelectByIdVO = (UserSelectByIdVO)session.getAttribute("user");
        if (userSelectByIdVO==null){
            return 2;
        }
        Weibo weibo = new Weibo();
        BeanUtils.copyProperties(weiboDTO,weibo);
        weibo.setCreated(new Date());
        weibo.setUserId(userSelectByIdVO.getId());
        weiboMapper.insert(weibo);
        return 1;
    }

    @GetMapping("selectIndex")
    public List selectIndex(HttpSession session){
        UserSelectByIdVO userSelectByIdVO = (UserSelectByIdVO)session.getAttribute("user");
        return weiboMapper.selectIndex();
    }

//    @GetMapping("selectById")
//    public WeiboDetailVO selectById(HttpSession session){
//        UserSelectByIdVO userSelectByIdVO = (UserSelectByIdVO)session.getAttribute("user");
//        return weiboMapper.selectById()
//    }
}
